<template>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" status-icon :rules="rules" label-width="auto"
        class="demo-ruleForm">
        <el-form-item label="用户名:" prop="username">
            <el-input v-model="ruleForm.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码:" prop="password">
            <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                登录
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
const ruleFormRef = ref<FormInstance>()




const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('不能为空'))
    } else {
        callback()
    }
}

const ruleForm = reactive({

    username: '',
    password: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    username: [{ validator: validatePass, trigger: 'blur' }],
    password: [{ validator: validatePass, trigger: 'blur' }],

})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            console.log(valid)
            if (ruleForm.username ==='admin' && ruleForm.password === '123456') {
                ElMessage({
                    message: '登录成功',
                    type: 'success',
                })
                setTimeout(() => {
                    window.location.href = '/'
                }, 500)
            } else {
                ElMessage({
                    message: '登录失败',
                    type: 'error',
                })
            }

        } else {
            console.log('登录失败!')
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>